home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 11 Learning / 01 Manslow / GPExample / CGPIfGreaterThanZeroNode.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-09  |  2.5 KB  |  108 lines

  1. //GPExample
  2. //Copyright John Manslow
  3. //29/09/2001
  4.  
  5. ////////////////////////////////////////////////////////////
  6. //Only when compiling under Windows
  7. #include "stdafx.h"
  8. #define new DEBUG_NEW
  9. ////////////////////////////////////////////////////////////
  10.  
  11. #include "CGPIfGreaterThanZeroNode.h"
  12. #include "CGPNode.h"
  13. #include "CGP.h"
  14. #include "assert.h"
  15.  
  16. extern CGPNode *pGPCreateRandomSubtree(void);
  17.  
  18. extern CGPNode **pPrototypeList;
  19. extern unsigned long ulNumberOfPrototypes;
  20.  
  21. CGPIfGreaterThanZeroNode IfGreaterThanZeroPrototype;
  22.  
  23. CGPIfGreaterThanZeroNode::CGPIfGreaterThanZeroNode()
  24. {
  25.     unsigned long i;
  26.     nType=7;
  27.  
  28.     pChildren=NULL;
  29.     ulNumberOfChildren=3;
  30.  
  31.     sprintf(psName,"IfGreaterThanZeroNode");
  32.  
  33.     nIsPrototype=1;
  34.     if(AddToPrototypeList())
  35.     {
  36.         nIsPrototype=0;
  37.         pChildren=new CGPNode*[ulNumberOfChildren];
  38.         for(i=0;i<ulNumberOfChildren;i++)
  39.         {
  40.             pChildren[i]=NULL;
  41.         }
  42.     }
  43.     dPrior=0.2;
  44. }
  45.  
  46. CGPIfGreaterThanZeroNode::~CGPIfGreaterThanZeroNode()
  47. {
  48. }
  49.  
  50. CGPNode *CGPIfGreaterThanZeroNode::pGetCopy(CGP* pGP)
  51. {
  52.     unsigned long i;
  53.  
  54.     CGPIfGreaterThanZeroNode *pNewNode=new CGPIfGreaterThanZeroNode;
  55.     assert(!(pNewNode==NULL));
  56.  
  57.     for(i=0;i<ulNumberOfChildren;i++)
  58.     {
  59.         if(pNewNode->pChildren[i]!=NULL)
  60.         {
  61.             delete pNewNode->pChildren[i];
  62.         }
  63.  
  64.         if(!nIsPrototype)
  65.         {
  66.             pNewNode->pChildren[i]=pChildren[i]->pGetCopy(pGP);
  67.         }
  68.         else
  69.         {
  70.             pNewNode->pChildren[i]=pGP->pGetRandomSubtree();
  71.         }
  72.     }
  73.     return (CGPNode*)pNewNode;
  74. }
  75.  
  76. double CGPIfGreaterThanZeroNode::dEvaluate(void)
  77. {
  78.     //This node the subtree connected to its second child if the evaluation of the subtree connected
  79.     //to its first child is positive, and evaluates the subtree connected to its third child otherwise. It
  80.     //returns one if the second subtree was evaluated and zero otherwise
  81.     if(pChildren[0]->dEvaluate()>0.0)
  82.     {
  83.         pChildren[1]->dEvaluate();
  84.         return 1.0;
  85.     }
  86.     pChildren[2]->dEvaluate();
  87.     return 0.0;
  88. }
  89.  
  90. char* CGPIfGreaterThanZeroNode::psGetString(char*pString)
  91. {
  92.     char *pSubString=new char[50000];
  93.     if(pString!=NULL && strlen(pString)<10000)
  94.     {
  95.         sprintf(pSubString,"");
  96.         pSubString=pChildren[0]->psGetString(pSubString);
  97.         sprintf(pString,"%s If(%s)",pString,pSubString);
  98.         sprintf(pSubString,"");
  99.         pSubString=pChildren[1]->psGetString(pSubString);
  100.         sprintf(pString,"%sThen(%s)",pString,pSubString);
  101.         sprintf(pSubString,"");
  102.         pSubString=pChildren[2]->psGetString(pSubString);
  103.         sprintf(pString,"%sElse(%s)",pString,pSubString);
  104.     }
  105.     delete []pSubString;
  106.     return pString;
  107. }
  108.